const path = require('path')

const HtmlWebpackPlugin = require('html-webpack-plugin')
const TerserWebpackPlugin = require('terser-webpack-plugin')

module.exports = (env) => {
    console.log('env-----------');
    console.log(env.goal);
    return {
        entry: {
            index: './src/index.js',
            another: './src/another-module.js'
        },
        output: {
            // 为不同的入口配置出口
            filename: 'scripts/[name].[contenthash].js',
            path: path.resolve(__dirname, 'dist'),
            clean: true,
            publicPath: 'http://localhost:8080/'
        },
        mode: env.production ? 'production' : 'development',
        plugins: [
            new HtmlWebpackPlugin({
                template: "./public/index.html",
                filename: "test.html",
                inject: true
            })
        ],
        module: {
            rules: [
                {
                    test: /\.css$/,
                    use: ['style-loader', 'css-loader']
                }, {
                    test: /\.js$/,
                    exclude: /node_modules/,
                    use: {
                        loader: 'babel-loader',
                        options: {
                            presets: ['@babel/preset-env'],
                            plugins: [
                                [
                                    '@babel/plugin-transform-runtime'
                                ]
                            ]
                        }
                    }
                }
            ]
        },
        devServer: {
            static: "./dist",
            open: true,
            host: 'local-ip',
            onListening(s) {
                let { address: ads, port } = s.server.address()
                console.log('http:' + ads + ':' + port);
            }
        },
        devtool: 'inline-source-map',
        optimization: {
            minimizer: [
                new TerserWebpackPlugin()
            ],
            splitChunks: {
                // chunks: 'all',
                cacheGroups: {
                    vendor: {
                        test: /[\\/]node_modules[\\/]/,
                        name: 'vendors',
                        chunks: 'all'
                    }
                }
            }
        }
    }
}


